{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8722c550",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import pandas as pd\n",
    "\n",
    "key = '2f9a26e66a3ae57f4fe9b7a99bdacf6c'\n",
    "\n",
    "def get_distance(start_loc, end_loc):\n",
    "    url = 'https://restapi.amap.com/v5/direction/driving'\n",
    "    params = {\n",
    "        'key': key,\n",
    "        'origin': start_loc,\n",
    "        'destination': end_loc\n",
    "    }\n",
    "    response = requests.get(url, params=params)\n",
    "    data = response.json()\n",
    "    distance = data['route']['paths'][0]['distance']\n",
    "    return distance\n",
    "\n",
    "def get_nearby_pois(location):\n",
    "    url = 'https://restapi.amap.com/v5/place/around'\n",
    "    params = {\n",
    "        'key': key,\n",
    "        'location': location,\n",
    "        'keywords': '快递网点',\n",
    "        'radius': 10000,\n",
    "        'page_size': 25,\n",
    "        'page_num': 1\n",
    "    }\n",
    "    all_pois = []\n",
    "    while True:\n",
    "        response = requests.get(url, params=params)\n",
    "        data = response.json()\n",
    "        pois = data['pois']\n",
    "        all_pois.extend(pois)        \n",
    "        if len(pois) < 25:\n",
    "            break\n",
    "        params['page_num'] += 1\n",
    "\n",
    "    return all_pois\n",
    "\n",
    "def collect_data(destinations):\n",
    "    df = pd.DataFrame(columns=['起点', '起点坐标', '目的地', '目的地坐标', '行车方案数', '最短距离'])\n",
    "    for start_point in destinations:\n",
    "        start_loc = ','.join(destinations[start_point]['location'])\n",
    "        pois = get_nearby_pois(start_loc)\n",
    "        for poi in pois:\n",
    "            end_point = poi['name']\n",
    "            end_loc = poi['location']\n",
    "            distance = get_distance(start_loc, end_loc)\n",
    "            df = df._append({\n",
    "                '起点': start_point,\n",
    "                '起点坐标': start_loc,\n",
    "                '目的地': end_point,\n",
    "                '目的地坐标': end_loc,\n",
    "                '行车方案数':1,\n",
    "                '最短距离': distance\n",
    "            }, ignore_index=True)\n",
    "    return df\n",
    "\n",
    "destinations = {\n",
    "    '中诚大厦': {'location': ['114.285327', '30.593856']},\n",
    "    '良友大厦': {'location': ['114.292385', '30.591015']},\n",
    "    '武银大厦': {'location': ['114.285327', '30.593856']}\n",
    "}\n",
    "\n",
    "data = collect_data(destinations)\n",
    "data.to_excel('D:/项目0.xlsx', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c131339a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e81a4ddf",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c7a0dd1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2cce99b8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eab43731",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ac3ce0c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33b5123c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28255c6f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a777e4a0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e84d253a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
